﻿using System.Collections.Generic;
using System.Resources;
using Localization.Repository;
using System.Text;
using System.Xml;
using System.IO;
using Localization.Repository.Models;
using System.Globalization;
namespace Localization.Repository
{
    /// <summary>
    /// Helps create the .resources files needed to make satellite assemblies
    /// <see cref="http://msdn.microsoft.com/en-us/library/aa905797.aspx"/>
    /// <see cref="http://msdn.microsoft.com/en-us/library/sb6a8618(v=VS.90).aspx">This describes the usage of satellite assemblies for .net 2.0 and higher</see>
    /// <see cref="http://msdn.microsoft.com/en-us/library/21a15yht%28VS.90%29.aspx"></see>
    /// </summary>
    internal sealed class ResourceFileHelper
    {
        public static bool ValidateResourceClassKeyName(string classKeyName)
        {
            return !(
                string.IsNullOrEmpty(classKeyName)
                || System.Text.RegularExpressions.Regex.IsMatch(
                    classKeyName,
                    "\\W\\b",
                    System.Text.RegularExpressions.RegexOptions.IgnoreCase));
        }

        private static IResourceWriter GetWriter(ResourceFileType type, string fileName)
        {
            switch (type)
            {
                case ResourceFileType.DotNet_Resource:
                    return new ResourceWriter(fileName);
                case ResourceFileType.DotNet_ResX:
                    return new ResXResourceWriter(fileName);
            }
            throw new System.InvalidOperationException();
        }

        public static void GenerateResourceFile(ResourceFileType type, string folderPath, string fileName, IEnumerable<ResourceValue> resources)
        {
            switch (type)
            {
                case ResourceFileType.DotNet_Resource:
                case ResourceFileType.DotNet_ResX:
                    using (IResourceWriter rw = GetWriter(type, folderPath + "/" + fileName))
                    {
                        if (resources != null)
                            foreach (ResourceValue r in resources)
                                rw.AddResource(r.Name, r.Value);

                        // generate resource file
                        rw.Generate();
                    }
                    break;
                case ResourceFileType.Android_ResourceFiles:
                case ResourceFileType.Android_LanguageFile:
                    string filePath = string.Format(@"{0}\{1}.xml", folderPath, fileName);

                    //XmlWriter writer = XmlTextWriter.Create();
                    
                    
                    //XmlDocument doc = new XmlDocument();

                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    sb.AppendLine("<resources>");
                    foreach (var r in resources)
                    {
                        sb.AppendLine(string.Format("<string name=\"{0}\">{1}</string>", r.Name, r.Value));
                    }
                    sb.AppendLine("</resources>");

                    File.WriteAllText(filePath, sb.ToString());
                    break;
                default:
                    throw new System.InvalidOperationException("Cannot create resource based on specified type: " + type.ToString());
            }
        }

        public static bool ValidateCulture(string cultureName)
        {
            try
            {
                CultureInfo ci = new CultureInfo(cultureName);
                return true;
            }
            catch { }
            return false;
        }
    }
}
